package com.zhida.smartcontrol.dialog.devicelistdialog;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.jay.tools.utils.SharedUtil;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Keys;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;
import com.zhida.smartcontrol.pagectrl.ProjectListBean;

import java.util.ArrayList;
import java.util.List;

public class DeviceListDialogPresenter extends BasePresenterImpl<DeviceListDialogContract.View> implements DeviceListDialogContract.Presenter {

    @Override
    public void requestProjectList() {
        String json = SharedUtil.getString(Keys.PROJECT_LIST, "");
        if (TextUtils.isEmpty(json)) {
            Params params = Params.newBuilder()
                    .add("projectName", "")
                    .build();
            mKalleCenter.get(Api.PROJECT_LIST, params, true, new KalleCallBack() {
                @Override
                public void onSuccess(String result, String msg) {
                    SharedUtil.putString(Keys.PROJECT_LIST, result);
                    parseProjectList(result);
                }

                @Override
                public void onFailed(String msg) {
                    mView.showMsg(msg);
                }
            });
        } else {
            parseProjectList(json);
        }

    }

    private void parseProjectList(String result) {
        List<ProjectListBean> zhengshi = new ArrayList<>();
        List<ProjectListBean> ceshi = new ArrayList<>();
        List<ProjectListBean> projectList = JSON.parseArray(result, ProjectListBean.class);
        for (int i = 0; i < projectList.size(); i++) {
                if (projectList.get(i).getCategoryTxt().equals("正式项目")) {
                    zhengshi.add(projectList.get(i));
                } else {
                    ceshi.add(projectList.get(i));
                }
        }
        projectList.clear();;
        projectList.addAll(zhengshi);
        projectList.addAll(ceshi);
        mView.showList(projectList);
    }

}
